<?php
//include'baps.php';
global $CRLF;
$CRLF='<br />';

function waerd($etable,$table){
  //header('Content-Type: text/html; charset=utf-8');
  echo'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<body>
<style type="text/css">
table.waerd{ width:200mm; height:290mm; font-family:Helvetica; border-style:none; border-collapse:collapse; border-spacing:0; }
table.waerd td{ border-style: none; }
#menu{ position:absolute; top:0; }
#wview{ position:absolute; top:2em; }
#wedit{ position:absolute; top:2em; }
#whide { position:absolute; top:2em; }
</style>
<form name="waeload" method="post" action="plugin/waerd/waerd.php?do=load">
<a href="#" onclick="finalwae();">Finalize</a>
 <a href="#" onclick="showf();">Preview</a>
 <a href="#" onclick="showe();">Edit</a>
 <a href="#" onclick="savewae();">Save..</a>
 Load: <input type="file" value="Load.." />
</form>',
    '<div id="wedit">',$etable,'</div>',
    '<div id="wview">',$table,'</div>',
    '<form name="waerdin" method="post" action="plugin/waerd/waerd.php?do=final">
<textarea name="view"  style="visibility:hidden;"></textarea>
<textarea name="edit"  style="visibility:hidden;"></textarea>
</form>
<script type="text/javascript">
//<![CDATA[
function modify(el){
  var vid="wf"+el.id.substr(2,el.id.length-2);
  //alert("change "+vid+" into "+el.value+" ("+el.id+")");
  document.getElementById(vid).innerHTML=el.value;
  el.setAttribute("value",el.value);
  alert(document.getElementById("wedit").innerHTML);
  alert(document.getElementById("wview").innerHTML);
}
function update(){
  document.waerdin.edit.value=document.getElementById("wedit").innerHTML;
  document.waerdin.view.value=document.getElementById("wview").innerHTML;
}
function finalwae(){
  update();
  document.waerdin.action="waerd.php?do=final";
  document.waerdin.submit();
}
function savewae(){
  update();
  alert(document.getElementById("wview").innerHTML);*/
  document.waerdin.action="waerd.php?do=save";
  document.waerdin.submit();
}
function showe(){
  document.getElementById("wview").style.visibility="hidden";
  document.getElementById("wedit").style.visibility="visible";
}
function showf(){
  document.getElementById("wview").style.visibility="visible";
  document.getElementById("wedit").style.visibility="hidden";
}

showe();
//]]>
</script>';
}

function waerdin($pa){
  global $CRLF;
  $pre=array(); $epre=array();
  $table=array('<table border=1 class="waerd"><tr>');
  $etable=array('<table border=1 class="waerd"><tr>');
  $suf=array(); $esuf=array();
  $count=0;
  foreach($pa as $value){
    if ($value==$CRLF){
      if(count($table)==1){ 
        $table=array('<table border=1 class="waerd"><tr>');
        $etable=array('<table border=1 class="waerd"><tr>');
      }
      else{ $table[]='</tr><tr>'; $etable[]='</tr><tr>'; }
    }
    else if(mb_substr($value,0,1,'UTF-8')=='$'){
      $t=substr($value,3);
      $et="<input type=\"text\" value=\"$t\" id=\"we$count\" onkeyup=\"modify(this);\">";
      $etable[]="<td>$et</td>";
      $dt="<div id=\"wf$count\">$t</div>";
      switch(substr($value,1,1)){
      case'$': $table[]='<td>i love u Renata</td>'; break;
      case'T':
	    $pre[]="<h2>$dt</h2>";
	    $epre[]="<input type=\"text\" value=\"$t\" id=\"we$count\" onkeyup=\"modify(this);\" />"; break;
      case'B': $table[]="<td><b>$dt</b></td>"; break;
      case'U': $table[]="<td><u>$dt</u></td>"; break;
      case'I': $table[]="<td><i>$dt</i></td>"; break;
    }}
    else{
	  $etable[]="<td><input type=\"text\" value=\"$value\" id=\"we$count\" onkeyup=\"modify(this);\" /></td>";
	  $table[]="<td><div id=\"wf$count\">$value</div></td>";
    }
    ++$count;
  }
  $table[]='</tr></table>';
  $etable[]='</tr></table>';
  $table=implode(array_merge($pre,$table,$suf));
  $etable=implode(array_merge($epre,$etable,$esuf));
  echo waerd($etable,$table);
}
if(isset($_GET['do'])){
  switch($_GET['do']){
  case'final':
    $hstart='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<body>';
    $hstop='</body></html>';
    if(isset($_GET['filename'])){ $filename=$_GET['filename']; }
    else{ $filename='waerd_output.htm'; }
    $data=$hstart.$_POST['view'].$hstop;
    $handle=fopen($filename,'w'); fwrite($handle,$data); fclose($handle);
    if (file_exists($filename)){
      header('Content-Description: File Transfer');
      header('Content-Type: application/octet-stream');
      header('Content-Disposition: attachment; filename='.basename($filename));
      header('Content-Transfer-Encoding: binary');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      header('Content-Length: '.filesize($filename)); //$file should really be the full path to the file.
      ob_clean(); flush(); readfile($filename); exit;
    }
    else{ echo$data; }
	break;
  case'save':
    if(isset($_GET['filename'])){ $filename=$_GET['filename']; }
    else{ $filename='waerd_output.wae'; }
    $data=''.$_POST['view'].'$wae$'.$_POST['edit'];
    $handle=fopen($filename,'w'); fwrite($handle,$data); fclose($handle);
	echo header('Location: waerd.php?do=load');
	echo 'grr';
    break;
  case'load':
    if(isset($_GET['filename'])){ $filename=$_GET['filename']; }
    else{ $filename='waerd_output.wae'; }
    $wae=explode('$wae$',file_get_contents($filename));
	echo waerd($wae[1],$wae[0]);
	break;
  }
}
else if(isset($_FILES['inputfile'])){
  $input=$_FILES['inputfile']['tmp_name'];
  $data=file_get_contents($input);
  if(0==strncmp($data,pack("CCC",0xef,0xbb,0xbf),3)){ $data=substr($data,3); }
  $data=explode("\n",$data);
  $tbl=array();
  $colcount=0;
  foreach($data as$row){
    $cc=count(explode(';',$row));
    if($cc>$colcount){ $colcount=$cc; }
  }
  foreach($data as$row){
    $cols=explode(';',$row);
    $padding=array();
    switch(substr($row,0,3)){
      case'$T$': break;
      default:
        $cc=count($cols);
        for($i= 0; $i<$colcount-$cc; ++$i){ $padding[]='&nbsp;'; }
    }
    $tbl=array_merge($tbl,$cols,$padding,array($CRLF));
  }
  echo waerdin($tbl);
}
else{ echo'<form name="inputform" method="post" action="plugin/waerd/waerd.php" enctype="multipart/form-data">
Choose a text file to process: <input type="file" name="inputfile" onchange="javascript:submit();"/>
</form>
<script type="text/javascript">
//<![CDATA[
document.inputform.inputfile.click();
//]]>
</script>'; }
?>